package com.example.springboot.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.example.springboot.common.dto.LoginDTO;
import com.example.springboot.common.result.Result;
import com.example.springboot.common.request.AdminListRequest;
import com.example.springboot.common.request.LoginRequest;
import com.example.springboot.common.request.PasswordRequest;
import com.example.springboot.entity.Admin;
import com.example.springboot.service.AdminService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

import static com.example.springboot.common.exception.ExceptionConstant.Captcha_ERROR;
import static com.example.springboot.common.exception.ExceptionConstant.Password_Duplication_ERROR;


@RestController
@RequestMapping("/admin")
public class AdminController {

    private static final String SESSION_KEY = "captcha";
    private static String current_code = "";  // 验证码session key

    @Autowired
    private AdminService adminService;

    /**
     * 管理员登录
     * @param loginRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest loginRequest) throws Exception {
        // 判断用户输入的验证码
        String code = loginRequest.getCode(); // 用户输入的验证码
        if(!code.equalsIgnoreCase(current_code)) {
            throw new Exception(Captcha_ERROR);
        }

        LoginDTO loginDTO = adminService.login(loginRequest);
        return Result.success(loginDTO);
    }

    /**
     * 分页模糊查询
     * @param adminListRequest
     * @return
     */
    @GetMapping("/page")
    public Result page(AdminListRequest adminListRequest) {
        return Result.success(adminService.page(adminListRequest));
    }

    /**
     * 添加管理员
     * @param admin
     * @return
     * @throws Exception
     */
    @PostMapping("/save")
    public Result save(@RequestBody Admin admin) throws Exception {
        adminService.save(admin);
        return Result.success();
    }

    /**
     * 根据id获取需要修改管理员的信息
     * @param id
     * @return
     */
    @GetMapping("/update/{id}")
    public Result getById(@PathVariable Integer id) {
        Admin admin = adminService.getById(id);
        return Result.success(admin);
    }

    /**
     * 更新管理员信息
     * @param admin
     * @return
     * @throws Exception
     */
    @PutMapping("/update")
    public Result editUser(@RequestBody Admin admin) throws Exception {
        adminService.update(admin);
        return Result.success();
    }

    /**
     * 修改管理员状态
     * @param admin
     * @return
     */
    @PutMapping("/updateStatus")
    public Result editUserStatus(@RequestBody Admin admin) {
        adminService.updateStatus(admin);
        return Result.success();
    }

    /**
     * 根据id删除管理员
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        adminService.deleteById(id);
        return Result.success();
    }

    /**
     * 修改密码
     * @param passwordRequest
     * @return
     */
    @PutMapping("/password")
    public Result changePassword(@RequestBody PasswordRequest passwordRequest) {

        adminService.changePassword(passwordRequest);
        return Result.success();
    }

    /**
     * 随机生成验证码
     * @param response
     * @throws Exception
     */
    @GetMapping("/captcha")
    public void generateCaptcha(HttpServletResponse response) throws IOException {
        // 创建一个线性验证码实例
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(100, 40, 4, 40); // 宽度、高度、字符和干扰线

        // 设置验证码内容和输出到http响应
        current_code = captcha.getCode();  // 获取验证码文本内容
        captcha.write(response.getOutputStream());  // 输出图片到浏览器

        // 将验证码内容存入响应头
        response.setHeader(SESSION_KEY, current_code);
    }

}
